﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Security;
using UCNKompetenceBors.Database;

namespace UCNKompetenceBors.Login
{
    public partial class resetPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Form.DefaultButton = btnResetPassword.UniqueID;

                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                if (Request.QueryString["uid"] != null)
                {
                    if (!IsPasswordResetLinkValid(Request.QueryString["uid"]))
                    {
                        panResetPasswordForm.Visible = false;
                        lblResetPasswordMessage.Text = GetLocalResourceObject("lblResetPasswordMessageText").ToString();
                    }
                }
                else
                {
                    panResetPasswordForm.Visible = false;
                    lblResetPasswordMessage.Text = GetLocalResourceObject("lblResetPasswordMessageText").ToString();
                }
            }
        }

        /// <summary>
        /// Checks whether the password reset link is valid.
        /// </summary>
        /// <param name="GUID">Specifies the GUID to be validated.</param>
        /// <returns></returns>
        private bool IsPasswordResetLinkValid(string GUID)
        {
            return dbLogin.PasswordResetLinkIsValid(GUID);
        }

        protected void btnResetPassword_Click(object sender, EventArgs e)
        {
            ResetPassword(txtPassword.Text);

            panResetPasswordForm.Visible = false;
            hyLinkConfirm.Visible = true;
            hyLinkConfirm.Text = GetLocalResourceObject("hyLinkConfirmText").ToString();
            lblResetPasswordMessage.Text = GetLocalResourceObject("lblResetPasswordMessageConfirmText").ToString();
        }

        /// <summary>
        /// Resets the password.
        /// </summary>
        /// <param name="newPassword">Spcifies the new password.</param>
        private void ResetPassword(string newPassword)
        {
            string userName = GetUserNameFromGUID();

            MembershipUser mu = Membership.GetUser(userName);
            mu.ChangePassword(mu.ResetPassword(), newPassword);

            DeletePasswordResetRequest();
        }

        /// <summary>
        /// Deletes the password reset request.
        /// </summary>
        private void DeletePasswordResetRequest()
        {
            dbLogin.DeletePasswordRecoveryRequest(Request.QueryString["uid"]);
        }

        /// <summary>
        /// Gets a user name from the specified GUID.
        /// </summary>
        /// <returns>Returns a string containing the user name.</returns>
        private string GetUserNameFromGUID()
        {
            return dbLogin.GetUserNameByGUID(Request.QueryString["uid"]);
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnResetPassword.Text = GetLocalResourceObject("btnResetPasswordText").ToString();
            lblConfirmPassword.Text = GetLocalResourceObject("lblConfirmPasswordText").ToString();
            lblResetPassword.Text = GetLocalResourceObject("lblResetPasswordText").ToString();

            string comparePassword = GetLocalResourceObject("compvalConfirmPasswordText").ToString();
            compvalConfirmPassword.Text = comparePassword;
            compvalConfirmPassword.ToolTip = comparePassword;
            compvalConfirmPassword.ErrorMessage = comparePassword;

            string confirmPassword = GetLocalResourceObject("rfvalConfirmPasswordText").ToString();
            rfvalConfirmPassword.Text = confirmPassword;
            rfvalConfirmPassword.ToolTip = confirmPassword;
            rfvalConfirmPassword.ErrorMessage = confirmPassword;

            string passwordValidate = GetLocalResourceObject("rfvalPasswordText").ToString();
            rfvalPassword.Text = passwordValidate;
            rfvalPassword.ToolTip = passwordValidate;
            rfvalPassword.ErrorMessage = passwordValidate;
        }
    }
}